<?php 
/*
* @package : User 
* @subpackage : User / Task
* @author : Pratik <pratik@fedobe.org>
* @copyright : Fedobe <http://my.fedobe.com> 
*/

if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tasks extends CI_Controller
{
	function __construct()
	{
		parent::__construct();

		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');
			$this->load->helper('download');
		}
		// loading library 
		$this->load->library('form_validation');
		$this->load->model('user/task_model');
		$this->load->model('user/attendance_model');
		$this->load->model('user/notification_model');
		$this->load->model('common_model');
		$this->load->library('encrypt');
		
		// making default timestamp to india
		date_default_timezone_set("Asia/Calcutta");
	}

	function index()
	{
		$meta['page_title'] = "Task Details";
		//getting the data's required :
		$data['pending_tasks'] = $this->task_model->get_pending_tasks();
		$data['achieved_tasks'] = $this->task_model->get_achieved_tasks();
		$data['completed_tasks'] = $this->task_model->get_completed_tasks();
		$this->load->view('common/header', $meta);
		$this->load->view('user/task',$data);
		$this->load->view('common/footer');
	}

	function create_task($edit_id = NULL)
	{
		if($this->ion_auth->in_group('manager'))
		{
			$this->form_validation->set_rules('user', 'User', 'required');
		}

		$this->form_validation->set_rules('project', 'Project Name', 'required');
		$this->form_validation->set_rules('taskgroup', 'Task Groups', 'required');
		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('desc', 'Description', 'required');
		$this->form_validation->set_rules('start_date', 'Start Date', 'required');
		$this->form_validation->set_rules('end_date', 'End Date', 'required');
		$this->form_validation->set_rules('priority', 'Priority', 'required');
		$edit_id = $this->input->post('edit_id');
		if(!empty($edit_id))
		{
			$this->form_validation->set_rules('edit_id', 'Id', 'required');
		}
		if($this->form_validation->run() == FALSE){
		}
		else
		{
			$for     = ($this->input->post('user')) ? $this->input->post('user') : $this->session->userdata('user_id');
			$edit_id = $this->input->post('edit_id');
			if(!empty($edit_id)){
				$update_insert = array(
					'project_id'  => $this->input->post('project'),
					'milestone_id'=> $this->input->post('milestone'),
					'taskgroup_id'=> $this->input->post('taskgroup'),
					'user_id'     => $for,
					'title'       => $this->input->post('title'),
					'desc'        => nl2br($this->input->post('desc')),
					'start_date'  => $this->input->post('start_date'),
					'end_date'    => $this->input->post('end_date'),
					'priority'    => $this->input->post('priority'),
					'created_by'  => $this->input->post('created_by'),
				);
				$this->task_model->update($update_insert,$edit_id);
				$this->session->set_flashdata('tasks_msg','This tasks has been updated');
				
				/*$noti_text =  $this->session->userdata('username').' update the task : <b>'.$this->input->post('title').'</b>';
		
				$update_insert = array(
				'desc' 		=> $noti_text,
				'project_id' =>	$this->input->post('project'),
				'notify_to' => $for,
				'created_date'	=>  date('Y-m-d'),
				'created_time' =>  date('H:i:s'),
				'is_read' 	=> 0,
				'url' 		=> 'user/tasks/task_detail/'.$edit_id,
				);
				
				$this->notification_model->create_notification($update_insert);*/
				
				
				redirect('user/tasks/task_detail/'.$edit_id);
			}
			else
			{
				$data = array(
					'project_id'  => $this->input->post('project'),
					'milestone_id'=> $this->input->post('milestone'),
					'taskgroup_id'=> $this->input->post('taskgroup'),
					'user_id'     => $for,
					'title'       => $this->input->post('title'),
					'desc'        => nl2br($this->input->post('desc')),
					'start_date'  => $this->input->post('start_date'),
					'end_date'    => $this->input->post('end_date'),
					'priority'    => $this->input->post('priority'),
					'created_by'  => $this->session->userdata('user_id'),
				);
				
				$insert_id = $this->task_model->create_task($data);
				$subtasks  = $this->input->post('subtask');
				/* filters the empty array removes the empty array */
				$subtasks  = array_filter($subtasks);
				
				if(isset($subtasks))
				{
					foreach($subtasks as $subtask){
						$insert_subtasks = array(
							'task_id'=> $insert_id,
							'user_id'=> $for,
							'desc'   => $subtask,
						);						
						$this->task_model->create_subtask($insert_subtasks);
						
				/*$noti_text =  $this->session->userdata('username').' created a subtasktask : <b>'.$subtask.'</b> to '.$this->common_model->get_username_id($for);
		
		$subtask_insert = array(
		'desc' 		=> $noti_text,
		'project_id' =>	$this->input->post('project'),
		'notify_to' => $for,
		'created_date'	=>  date('Y-m-d'),
		'created_time' =>  date('H:i:s'),
		'is_read' 	=> 0,
		'url' 		=> 'user/tasks/task_detail/'.$insert_id,
		);
		
		
		$this->notification_model->create_notification($subtask_insert);*/
						
					}
				}
			}
			/**
			* 
			* @var notification
			* 
			*/
			
			// for user		
		/*$noti_text =  $this->session->userdata('username').' assigned a task : <b>'.$this->input->post('title').'</b> to '.$this->common_model->get_username_id($for);
		
		$user_insert = array(
		'desc' 		=> $noti_text,
		'project_id' =>	$this->input->post('project'),
		'notify_to' => $for,
		'created_date'	=>  date('Y-m-d'),
		'created_time' =>  date('H:i:s'),
		'is_read' 	=> 0,
		'url' 		=> 'user/tasks/task_detail/'.$insert_id,
		);
		
		
		$this->notification_model->create_notification($user_insert);*/
			// for user end here			
			/*$noti_text =  $this->session->userdata('username').' created a task : "'.$this->input->post('title').'" ';
		
		$user_insert = array(
		'desc' 		=> $noti_text,
		'project_id' =>	$this->input->post('project'),		
		'notify_to' => $this->session->userdata('user_id'),
		'created_date'	=>  date('Y-m-d'),
		'created_time' =>  date('H:i:s'),
		'is_read' 	=> 0,
		'url' 		=> 'user/tasks/task_detail/'.$insert_id,
		);
		
		$this->notification_model->create_notification($user_insert);	*/
			
			
			redirect('user/tasks/milestones_details/'.$this->input->post('project'));
		}
		$this->data['users'] = $this->ion_auth->users()->result();
		$meta['page_title'] = "Create a New Task";
		$this->data['projects'] = $this->task_model->get_all_projects();		
		$this->data['milestones'] = $this->task_model->get_default_milestone();
		$this->data['taskgroups'] = $this->task_model->get_default_taskgroups();

		$this->load->view('common/header', $meta);
		$this->load->view('user/create-task',$this->data);
		$this->load->view('common/footer');
	}
	
	function get_project_manager()
	{		
		echo $this->task_model->get_project_manager($_REQUEST['project_id']);	
	}	
	
	function get_custom_taskgroup_by_projectid(){
		$id = $_REQUEST['project_id'];
		$ctgs = $this->task_model->get_custom_taskgroup_by_projectid($id);
		foreach($ctgs as $ctg){
			echo '<option value="">asd</option>';
		}
	}

	function edit($edit_id)
	{
		$this->data['users'] = $this->ion_auth->users()->result();
		$this->data['edit'] = $data = $this->task_model->edit($edit_id);
		$this->data['projects'] = $this->task_model->get_all_projects();
		$this->data['milestones'] = $this->task_model->get_milestones();
		$this->data['taskgroups'] = $this->task_model->get_taskgroups();
		
		$meta['page_title'] = "Edit Task";
		$this->load->view('common/header', $meta);
		$this->load->view('user/create-task',$this->data);
		$this->load->view('common/footer');
	}

	function task_detail($edit_id)
	{		
		// check access level for projects			
		if($this->ion_auth->is_client($this->session->userdata('user_id')))
		{
			// get all the task related to that client only
			$get_tasks_list = $this->task_model->tasks_access($this->session->userdata('user_id'));			
			foreach($get_tasks_list as $value ){
				foreach($value as $val)  {
					$check[] = $val['id'];
				}
			}			
			if (!in_array($edit_id,$check)){
				redirect('errorpage/unauthorized');
			}						
		}		
		
		$data['tasks'] = $this->task_model->get_task($edit_id);
		$data['notes'] = $this->task_model->get_note_by_id($edit_id);
		$data['task_id'] = $this->uri->segment(4);
		$data['subtasks'] = $this->task_model->get_subtask($edit_id);
		$data['trashed_subtasks'] = $this->task_model->get_trashed_subtask($edit_id);
		$data['special_notes'] = $this->task_model->get_special_notes($edit_id);
		$data['feedbacks']	= $this->task_model->get_feedbacks($edit_id);
		
		//gett the followers 
		$data['users'] = $this->ion_auth->users()->result();
		$data['followers']	= $this->task_model->get_task_followers($edit_id);	
		
		//print_r($data['followers']);
		//$data['not_followers']	= $this->task_model->get_task_followers($edit_id,$data['not_followers']);
		
		$meta['page_title']= $data['tasks']['name'].' :: '.$data['tasks']['title'];
		$meta['feedback_percentage'] =  $this->task_model->feedback_percentage($edit_id);
		
		$this->load->view('common/header', $meta);
		$this->load->view('user/task-detail',$data);		
		$this->load->view('common/footer');
	}

	function delete_task($task_id,$project_id)
	{
		/*$noti_text =  $this->session->userdata('username').' deleted a task : <b>'.$this->get_task_name($task_id).'</b>';
		
		$delete = array(
		'desc' 			=> 	$noti_text,
		'project_id' 	=>	$project_id,
		'notify_to' 	=> 	$this->session->userdata('user_id'),
		'created_date'	=>  date('Y-m-d'),
		'created_time' 	=>  date('H:i:s'),
		'is_read' 		=> 	0,
		'url' 			=> 	"",
		);
		
		
		$this->notification_model->create_notification($delete);*/
		$this->task_model->delete_task($task_id);
		$this->session->set_flashdata('note_message', 'Note deleted successfully');
		redirect('user/tasks/milestones_details/'.$project_id);
	}
	
	function get_task_name($id =NULL){
		return $this->task_model->get_task_name($id);
	}

	/* notes function starts here */
	function add_note()
	{
		$insert =array(
			'user_id'		=> $this->session->userdata('user_id'),
			'task_id'	   	=>	$_REQUEST['taskid'],
			'note'   	   	=>	nl2br($_REQUEST['note']),
			'note_type'   	=>	$_REQUEST['note_type'],			
		);
		
		$this->task_model->add_note($insert);
		$this->session->set_flashdata('note_message', 'Note succesfully added');
	}

	function delete_note($note_id,$task_id)
	{
		$this->task_model->delete_note($note_id);
		$this->session->set_flashdata('note_message', 'Note delted successfully');
		redirect('user/tasks/task_detail/'.$task_id,'location');
	}
	
	function edit_note($id,$task_id){
		$data['task_id'] = $task_id;
		$data['notes'] = $this->task_model->get_note($id);
		$this->load->view('user/edit_note',$data);
	}
	
	function update_note(){
		$tasks_id = $this->input->post('task_id');
		$note_type = $this->input->post('note_type');
		$id = $this->input->post('note_id');
		$set = array(
		'note_type' => $note_type,
		'note' 		=> nl2br($this->input->post('note')),
		);
		
		$this->task_model->update_note($id,$set);
		redirect('user/tasks/task_detail/'.$tasks_id,'location');
	}

	function task_achieved()
	{
		
		$update = array(
			'is_achieved'=> 1,
			'completed_percentage' => 100,
		);
		$check = array(
			'id'=> $_REQUEST['taskid']
		);
		$this->task_model->is_achieved($update,$check);
	}

	function task_unachieved()
	{
		$update = array(
			'is_achieved'=> 0,
			'completed_percentage' => 0,
		);
		$check = array(
			'id'=> $_REQUEST['taskid']
		);
		
		$this->task_model->task_unachieved($update,$check);
	}

	function task_closed()
	{
		
		$update = array(
			'is_completed'=> 1
		);
		$check = array(
			'id'=> $_REQUEST['taskid']
		);
		$this->task_model->task_closed($update,$check);

	}

	function task_open()
	{
		// $task_id = $_REQUEST['taskid'];
		$update = array(
			'is_completed'=> 0
		);
		$check = array(
			'id'=> $_REQUEST['taskid']
		);
		$this->task_model->task_open($update,$check);
	}

	function check_is_achieved()
	{
		$task_id = $_REQUEST['taskid'];
		$value   = $this->task_model->check_is_achieved($task_id);
		echo $value['is_achieved'];

	}

	function check_is_completed()
	{
		$task_id = $_REQUEST['taskid'];
		$value   = $this->task_model->check_is_completed($task_id);
		echo $value['is_completed'];
	}

	// sub tasks functions
	function create_subtask($task_id)
	{
		//echo $this->input->post('subtask');
		//exit;
		$insert_subtasks = array(
			'task_id'    => $task_id,
			'user_id'    => $this->session->userdata('user_id'),
			'desc'       => $this->input->post('subtask'),
			'priority'   => $this->input->post('priority'),
			'time_spent' => $this->input->post('hr').':'.$this->input->post('min').':'.$this->input->post('sec'),
			'is_achieved'=>	0
		);
		$value = $this->task_model->create_subtask($insert_subtasks);
		
	/*$noti_text =  $this->session->userdata('username').' created a subtasktask : <b>'.$subtask.'</b> on Task ::'.$this->input->post('task_name');
		
		$subtask_insert = array(
		'desc' 		=> $noti_text,
		'project_id' =>	$this->input->post('project_id'),
		'notify_to' => $this->session->userdata('user_id'),
		'created_date'	=>  date('Y-m-d'),
		'created_time' =>  date('H:i:s'),
		'is_read' 	=> 0,
		'url' 		=> 'user/tasks/task_detail/'.$insert_id,
		);
		
		$this->notification_model->create_notification($subtask_insert);*/
		redirect('user/tasks/task_detail/'.$task_id);	
	}
	

	function create_subtask_heading($task_id)
	{
		$insert_subtasks = array(
			'task_id'   => $task_id,
			'user_id'   => $this->session->userdata('user_id'),
			'desc'      => $this->input->post('subtask'),
			'is_heading'=> 1,
		);
		$value = $this->task_model->create_subtask($insert_subtasks);
		//	$this->load->view('common/header');
		redirect('user/tasks/task_detail/'.$task_id);
		//$this->form_validation->set_rules('subtask', 'Subtask', 'required');
	}

	function delete_subheading($id,$task_id)
	{
		//echo $id,$task_id;
		//exit;
		echo  $value = $this->task_model->delete_subtask($id);
		redirect('user/tasks/task_detail/'.$task_id);
	}

	function subtask_achieved()
	{
		$subtask_id = $_REQUEST['subtask_id'];
		echo  $value = $this->task_model->subtask_achieved($subtask_id);
	}

	function subtask_unachieved()
	{
		$subtask_id = $_REQUEST['subtask_id'];
		$value      = $this->task_model->subtask_unachieved($subtask_id);
	}

	function check_subtask_is_achieved()
	{
		$subtask_id = $_REQUEST['subtask_id'];
		$value      = $this->task_model->check_subtask_is_achieved($subtask_id);
		echo $value['is_achieved'];
	}

	function delete_subtask($task_id,$delete_id)
	{
		$this->task_model->delete_subtask($delete_id);
		//$this->db->delete('mytable', array('id' => $delete_id));
		redirect('user/tasks/task_detail/'.$task_id,'location');
	}

	function edit_subtask($id)
	{
		$data['subtasks'] = $this->task_model->edit_subtask($id);
		$this->load->view('user/edit-subtask',$data);
	}
	
	function move_trash_subtask($taskid,$subtask_id){
		$this->task_model->move_trash_subtask($subtask_id);
		redirect('user/tasks/task_detail/'.$taskid,'location');		
	}
	
	function restore_trash_subtask($taskid,$subtask_id){
		$this->task_model->restore_trash_subtask($subtask_id);
		redirect('user/tasks/task_detail/'.$taskid,'location');		
	}

	function update_subtask()
	{
		$time_spent = $this->input->post('hr').':'.$this->input->post('min').':'.$this->input->post('sec');
		
		$update = array(
			'desc'    => $this->input->post('desc'),
			'priority'=>$this->input->post('priority'),
			'time_spent' => $time_spent,
		);
		
		$id      =  $this->input->post('id');
		$task_id = $this->input->post('taskid');
		$this->task_model->update_subtask($update,$id,$task_id);
		redirect('user/tasks/task_detail/'.$task_id,'location');
	}
	/* Project function starts here*/

	function projects()
	{
		$this->data['users'] = $this->ion_auth->users()->result();
		$this->data['projects'] = $this->task_model->get_all_projects();
		$this->data['departments'] = $this->ion_auth_model->departments()->result();
		$this->data['clients'] = $this->task_model->get_clients();
		$meta['page_title'] = "Projects";

		$this->form_validation->set_rules('user', 'User', 'required');
		$this->form_validation->set_rules('department_id', 'Department', 'required');
		$this->form_validation->set_rules('name', 'Name', 'required|is_unique[projects.name]');
		$this->form_validation->set_rules('desc', 'Description', 'required');
		$this->form_validation->set_rules('start_date', 'Start Date', 'required');
		$this->form_validation->set_rules('due_date', 'Due Date', 'required');

		if($this->form_validation->run() == TRUE){

			$insert = array(
				'name'        	=> $this->input->post('name'),
				'department_id' => $this->input->post('department_id'),
				'desc'        	=> $this->input->post('desc'),
				'start_date'  	=> $this->input->post('start_date'),
				'end_date'    	=> $this->input->post('due_date'),
				'created_on'  	=> date('Y-m-d'),
				'created_by'  	=> $this->session->userdata('user_id'),
				'assigned_to' 	=> $this->input->post('user'),
				'is_completed'  => 0,
				'client_id' 	=> $this->input->post('client_id'),
				
			);
			$project_id = $this->task_model->create_project($insert);
			$this->session->set_flashdata('msg','Project has been created successfully');
			//redirect('user/tasks/milestones/'.$project_id,'location');
			redirect('user/tasks/projects_details','location');
		}

		$this->load->view('common/header',$meta);
		$this->load->view('user/projects',$this->data);
		$this->load->view('common/footer');
	}
	
	/**
	* my projects section
	* @author : pratik <prartik@fedobe.org>
	* 
*/
	function my_projects(){
		$meta['page_title'] = 'My Projects';		
		$data['my_projects'] =  $this->task_model->my_projects();		
		$data['shared_projects'] =  $this->task_model->shared_projects();
		$this->load->view('common/header',$meta);
		$this->load->view('user/my_projects',$data);
		$this->load->view('common/footer');
		
	}
	
	public function add_fav_project(){		
		$this->task_model->add_fav_project($_REQUEST['id']);
	}
	
	public function remove_fav_project(){		
		$this->task_model->remove_fav_project($_REQUEST['id']);
	}
	
	
	public function add_fav_tasks(){		
		$this->task_model->add_fav_tasks($_REQUEST['id']);
	}
	
	public function remove_fav_tasks(){		
		$this->task_model->remove_fav_tasks($_REQUEST['id']);
	}
	
	function edit_project($id)
	{
		$meta['page_title'] = 'Edit ';
		$this->data['users'] = $this->ion_auth->users()->result();
		$this->data['edit']=$this->task_model->getProjectDetails($id);
		$this->data['departments'] = $this->ion_auth_model->departments()->result();
		$this->data['clients'] = $this->task_model->get_clients();
		$this->form_validation->set_rules('user', 'User', 'required');
		$this->form_validation->set_rules('department_id', 'Department', 'required');
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('desc', 'Description', 'required');
		$this->form_validation->set_rules('start_date', 'Start Date', 'required');
		$this->form_validation->set_rules('due_date', 'Due Date', 'required');

		if($this->form_validation->run() == TRUE){

			$update = array(
				'department_id'     => $this->input->post('department_id'),
				'name'        => $this->input->post('name'),				
				'desc'        => $this->input->post('desc'),
				'start_date'  => $this->input->post('start_date'),
				'end_date'    => $this->input->post('due_date'),
				'created_on'  => date('Y-m-d'),
				'created_by'  => $this->session->userdata('user_id'),
				'assigned_to' => $this->input->post('user'),
				'is_completed'=> 0,
				'client_id'   => $this->input->post('client_id'),
				
			);
			//print_r($insert);
			//exit;
			$this->task_model->update_project($update,$id);
			$this->session->set_flashdata('msg','Project has been update successfully');
			redirect('user/tasks/projects_details','location');
		}
		$this->load->view('common/header',$meta);
		$this->load->view('user/edit-projects',$this->data);
		$this->load->view('common/footer');
	}
	
	function project_completed(){
		//echo $_REQUEST['project_id'];
		$this->task_model->project_completed($_REQUEST['project_id']);
	}
	
	function project_incompleted(){
		//echo $_REQUEST['project_id'];
		$this->task_model->project_incompleted($_REQUEST['project_id']);
	}
	
	function projects_details()
	{
		$meta['page_title'] = "Project details";		
		$data['completed_projects'] = $this->task_model->get_completed_projects();
		$data['departments'] = $this->ion_auth_model->departments()->result();
		
		$this->load->view('common/header',$meta);
		$this->load->view('user/projects-details',$data);
		$this->load->view('common/footer');
	}
	
	function completed_projects()
	{
		$meta['page_title'] = "Project details";		
		$data['completed_projects'] = $this->task_model->get_completed_projects();
		$data['departments'] = $this->ion_auth_model->departments()->result();
		
		$this->load->view('common/header',$meta);
		$this->load->view('user/completed-project',$data);
		$this->load->view('common/footer');
	}
	
	function sorting_projects()
	{

		$action             = mysql_real_escape_string($_POST['action']);
		$updateRecordsArray = $_POST['recordsArray'];

		$this->task_model->sorting_projects($action,$updateRecordsArray);

	}
	/* Milestone function starts here*/
	function milestones($project_id)
	{
		$data['projects'] = $this->task_model->get_project_name_by_id($project_id);
		$data['milestones'] = $this->task_model->get_milestone_by_project_id($project_id);
		$data['default_milestones'] = $this->task_model->get_default_milestone($project_id);
		$meta['page_title'] = "Projects";
		$this->load->view('common/header',$meta);
		$this->load->view('user/milestones',$data);
		$this->load->view('common/footer');
	}

	function default_milestone()
	{
		$data['milestones'] = $this->task_model->get_default_milestone();
		$meta['page_title'] = "Projects";
		$this->load->view('common/header',$meta);
		$this->load->view('user/default-milestones',$data);
		$this->load->view('common/footer');
	}

	function create_default_milestone()
	{
		$this->form_validation->set_rules('name', 'Name', 'required|is_unique[milestones.name]');
		$this->form_validation->set_rules('desc', 'Description', 'required');

		if($this->form_validation->run() == FALSE){
			redirect('user/tasks/default_milestone');
		}
		else
		{
			$insert = array(
				'project_id'=> 0,
				'name'      => $this->input->post('name'),
				'desc'      => $this->input->post('desc'),
				'created_on'=> date('Y-m-d'),
				'created_by'=> $this->session->userdata('user_id'),
				'is_default'=> 1,
			);
			$this->task_model->create_milestone($insert);
			redirect('user/tasks/default_milestone');
		}
	}
	function create_milestone($project_id)
	{
		$this->form_validation->set_rules('name', 'Name', 'required|is_unique[projects.name]');
		$this->form_validation->set_rules('desc', 'Description', 'required');

		if($this->form_validation->run() == FALSE){
			$data['projects'] = $this->task_model->get_project_name_by_id($project_id);

			$meta['page_title'] = "Create MileStone";
			$data['milestones'] = $this->task_model->get_milestone_by_project_id($project_id);
			$this->load->view('common/header',$meta);
			$this->load->view('user/create-milestones',$data);
			$this->load->view('common/footer');
		}

		else
		{
			$project_id = $this->input->post('project_id');
			$insert     = array(
				'project_id'=> $this->input->post('project_id'),
				'name'      => $this->input->post('name'),
				'desc'      => $this->input->post('desc'),
				'created_on'=> date('Y-m-d'),
				'created_by'=> $this->session->userdata('user_id'),
				'is_default'=> 0,
			);
			//print_r($insert);

			$this->task_model->create_milestone($insert);
			redirect('user/tasks/milestones/'.$project_id,'location');
		}
	}

	function get_milestone_by_project_id()
	{
		$project_id      = $_REQUEST['project_id'];
		$default_options =  $this->task_model->get_default_milestone();
		$custom_options  =  $this->task_model->get_milestone_by_project_id($project_id);

		echo '<select name="milestone" class="chzn-select" id="milestone1">';
		foreach($default_options as $default_option){
			echo '<option value="'.$default_option->id.'">'.$default_option->name.'</option>';
		}
		foreach($custom_options as $custom_option){
			echo '<option value="'.$custom_option->id.'">'.$custom_option->name.'</option>';
		}
		echo '</select>';
		
		
		
	}

	/* default taskgroup */

	function get_taskgroup_by_project_id()
	{
		/*$milestone_id    = $_REQUEST['milestone_id'];*/
		$project_id      = $_REQUEST['project_id'];
		$default_options =  $this->task_model->get_default_taskgroups();
		$custom_options  =  $this->task_model->get_custom_taskgroups($project_id);

		echo '<select  id="" name="taskgroup" >';
		foreach($default_options as $default_option){
			echo '<option value="'.$default_option->id.'">'.$default_option->name.'</option>';
		}
		foreach($custom_options as $custom_option){
			echo '<option value="'.$custom_option->id.'">'.$custom_option->name.'</option>';
		}
		echo '</select>';


	}

	function default_taskgroup()
	{
		$data['taskgroups'] = $this->task_model->get_default_taskgroups();
		$meta['page_title'] = "Projects";
		$this->load->view('common/header',$meta);
		$this->load->view('user/default-task-groups',$data);
		$this->load->view('common/footer');
	}

	function create_default_taskgroup()
	{
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('desc', 'Description', 'required');

		if($this->form_validation->run() == FALSE){
			//redirect('user/tasks/default_taskgroup');
			$data['taskgroups'] = $this->task_model->get_taskgroups();
			$meta['page_title'] = "Projects";
			$this->load->view('common/header',$meta);
			$this->load->view('user/default-task-groups',$data);
			$this->load->view('common/footer');
		}
		else
		{
			$insert = array(
				'project_id'  => 0,
				'milestone_id'=> 0,
				'name'        => $this->input->post('name'),
				'desc'        => $this->input->post('desc'),
				'created_on'  => date('Y-m-d'),
				'created_by'  => $this->session->userdata('user_id'),
				'is_default'  => 1,
			);
			$this->task_model->create_taskgroups($insert);

			redirect('user/tasks/default_taskgroup');
		}
	}

	/* create custom task groups*/

	function custom_taskgroup($project_id,$milestone_id)
	{
		$data['default_taskgroups'] = $this->task_model->get_default_taskgroups();
		$data['custom_taskgroups'] = $this->task_model->get_taskgroup_by_id($project_id,$milestone_id);

		$data['project_id'] = $project_id;
		$data['milestone_id'] = $milestone_id;

		$meta['page_title'] = "Task Groups";
		$this->load->view('common/header',$meta);
		$this->load->view('user/create-task-group',$data);
		$this->load->view('common/footer');
	}

	function create_custom_taskgroup($project_id)
	{
		$insert = array(
				'project_id'  => $project_id,
				'milestone_id'=> 0,
				'name'        => $this->input->post('name'),
				'desc'        => $this->input->post('desc'),
				'created_on'  => date('Y-m-d'),
				'created_by'  => $this->session->userdata('user_id'),
				'is_default'  => 0
			);
			
			$this->task_model->create_taskgroups($insert);
			redirect('user/tasks/milestones_details/'.$project_id,'location');
		
	}
	
	function get_custom_task_group($id){		
		$data['custom_tasks'] = $this->task_model->get_custom_task_group($id);
		$this->load->view('user/edit_custom_taskgroup',$data);
	}
	function update_custom_task_group($id,$project_id){
		$input = $this->input->post();
			
		$update = array(
		'name' => $input['name'],
		'desc' =>$input['desc'],
		);
		$this->task_model->update_custom_task_group($id,$update);
		redirect('user/tasks/milestones_details/'.$project_id);		
	}

	/*
	* @param undefined $project_id
	* 
	*/
	function milestones_details($project_id)
	{
		if($this->ion_auth->is_client($this->session->userdata('user_id')))
		{
			// get all the task related to that client only
			$get_project_list = $this->task_model->project_access($this->session->userdata('user_id'));
			foreach($get_project_list as $get_project_list){
				$check[] = $get_project_list['id'];
			}				
			if (!in_array($project_id,$check)){
				redirect('errorpage/unauthorized');
			}
		}	
		
		$meta['page_title'] = "Task list details";		
		$data['projects'] = $this->task_model->get_project_name_by_id($project_id);	
		
		$data['milestones'] = $this->task_model->get_default_milestone();

		$data['taskgroups'] = $this->task_model->get_default_taskgroups();
		$data['custom_taskgroups'] = $this->task_model->get_custom_taskgroups($project_id);
		
		$data['followers']	= $this->task_model->get_project_followers($project_id);
		$data['project_manager']	= $this->task_model->get_project_manager($project_id);		
		
		$data['users'] = $this->ion_auth->users()->result();
		$this->load->view('common/header',$meta);
		$this->load->view('user/milestones-details',$data);
		$this->load->view('common/footer');
		
	}

	function sorting_subtasks()
	{

		$action             = mysql_real_escape_string($_POST['action']);
		$updateRecordsArray = $_POST['recordsArray'];

		$this->task_model->sorting_subtask($action,$updateRecordsArray);
	}

	function completed_percentage()
	{
		//$completed = 0;
		$completed = $_REQUEST['comp'];
		//exit;
		$this->task_model->completed_percentage($completed ,$_REQUEST['id']);
	}
	
	function time_spent(){
		$hr = $_REQUEST['hr'];
		$min = $_REQUEST['mins'];
		$sec = $_REQUEST['sec'];
		$time_spent = $hr.$min.$sec;
		$id = $_REQUEST['id'];
		//exit;
		$this->task_model->time_spent($time_spent,$id);
	}
	
	function auto_time_update(){
		//$id = $_REQUEST['id'];
		$id = 1;
		$tot_time = $this->task_model->auto_time_update($id);		
		$tt_tot = 0;
		foreach($tot_time as $tt){
			$tt_explode = explode(':',$tt->time_spent);
		 	$tt_hr 		= $tt_explode [0]*60*60;
			$tt_min		= $tt_explode [1]*60;
			
			$tt_tot += $tt_hr + $tt_min;
		}
		
		$hours 			= floor($tt_tot / 3600);
		$minutes 		= floor(($tt_tot / 60) % 60);		
		echo $sumtime = $hours .':'.$minutes;
		//echo $tt_tot;
		
	}
	
	function get_subtask_for_worksheet($id,$task_id){
		$data['subtasks'] = $this->task_model->get_subtask_for_worksheet($id);
		$data['project'] = $this->task_model->get_task_project($task_id);
		$data['tasks_id'] = $task_id;
		$this->load->view('user/add_worksheet',$data);		
	}
	
	function get_subtask_for_todo($id,$task_id){
		$data['subtasks'] = $this->task_model->get_subtask_for_worksheet($id);
		$data['project'] = $this->task_model->get_task_project($task_id);
		$data['users'] = $this->ion_auth->users()->result();
		$data['tasks_id'] = $task_id;
		$this->load->view('user/add_todo',$data);		
	}
	
	function add_worksheet($id,$task_id){
		
		$time_spent = $this->input->post('hour').':'.$this->input->post('min').':'.	$this->input->post('sec');
		$date = explode('-',$this->attendance_model->get_server_date());
		$year_id = str_replace('20','',$date[0]);
		$month_id = $date[1];
		$date_id = $date[2];
		$pID = $this->input->post('project_id');
		
		$desc = '<b><a href="'.site_url("user/tasks/milestones_details/".$pID).'">'.$this->input->post('project_name').'</a></b> : <a href="'.site_url("/user/tasks/task_detail/".$task_id).'">'.$this->input->post('tasks_title').'</a> - '.$this->input->post('desc');
		
		// $this->input->post('desc').' - <span class="text-error">'. $this->input->post('project_name').'</span> :'.$this->input->post('tasks_title');
		
		$insert = array(
		'desc' 			=> 	$desc,
		'time_spent'	=>	$time_spent,
		'year_id' 		=>	$year_id,
		'month_id'		=> 	$month_id,
		'user_id'		=> 	$this->session->userdata('user_id'),
		'date_id'		=> 	$date_id,
		);
		
		$return_value = $this->task_model->add_worksheet($insert,$id);
		
		if($return_value == 'FALSE'){			
			$this->session->set_flashdata('worsheet_msg', '<i class="icon-wrong"></i>Duplicate data. Already in worksheet');
		}		
		redirect('user/tasks/task_detail/'.$task_id,'location');
	}
	
	// Reply funtions
	function post_reply()
	{
		//ajax used 
		/*
		echo $note_id = $_REQUEST['note_id'];
		echo $replies = $_REQUEST['replies'];*/
		
		$insert = array(
		'user_id' => $this->session->userdata('user_id'),
		'note_id' => $_REQUEST['note_id'],
		'reply'	  => nl2br($_REQUEST['replies']),		
		);		
		$this->task_model->add_reply($insert);
		//if()
		//{
		 //$this->session->set_flashdata('reply_msg', '<i class="icon-ok"></i> Duplicate data. Already in worksheet');	
		//}
		
	}
	function delete_reply($task_id,$id){
		$this->task_model->delete_reply($id);
		redirect('user/tasks/task_detail/'.$task_id,'location');
	}
	
	
	function feedback($id,$for_user,$emo){
		$data['emo']= $emo;
		$data['tasks_id'] = $id;
		$data['for'] = $for_user;		
		$this->load->view('user/add_feedback',$data);
	}
	function add_feedback(){
		$tasks_id = $this->input->post('task_id');
		$insert  = array(
		'task_id' 	=> $tasks_id,
		'user_id' 	=> $this->input->post('for_id'),
		'for' 		=> $this->session->userdata('user_id'),
		'emotion' 	=> $this->input->post('emo'),
		'feedback'	=> nl2br($this->input->post('feedback')),
		);		
		$this->task_model->add_feedback($insert);
		redirect('user/tasks/task_detail/'.$tasks_id,'location');
	}
	
	function delete_feedback(){				
		$this->task_model->delete_feedback($_REQUEST['id']);
	}
	/**
	* special notes of each subtaks
	* Author: Arpan Pratik Kumar
	*/
	
	function special_notes($subtask_id){
		$posted_by = $this->session->userdata('user_id');
		$data['subtask_details'] = $this->task_model->get_subtask_by_id($subtask_id);
		$data['special_notes'] = $this->task_model->get_special_notes($subtask_id);
		// get all the special notes 
		$this->load->view('user/special_note',$data);		
	}
	
	function save_discussion(){
		$note = nl2br($_REQUEST['note']);
		$subtaskid = $_REQUEST['subtaskid'];		
		$insert_data = $this->task_model->save_discussion($note,$subtaskid);
		
		/*// notification 
		
		$noti_text = $this->session->userdata('username').' commented "'.$_REQUEST['note'].'" on task : <b>'.$this->input->post('title').'</b> ';
		
		$user_insert = array(
		'desc' 		=> $noti_text,
		//'project_id' =>	$this->input->post('project'),
		'notify_to' => $this->session->userdata('user_id'),
		'created_on'=> date('Y-m-d H:m:i',time()),
		'is_read' 	=> 0,
		'url' 		=> 'user/tasks/task_detail/'.$insert_id,
		);
		
		$this->notification_model->create_notification($user_insert);*/
		
		if($insert_data){
			echo "Note added successfully";
		}else {
			echo "Some error occured. Please add discussion again";
		}
		
	}
	
	function delete_discussion(){
		$id = $_REQUEST['id'];
		$this->task_model->delete_discussion($id);
			
		
	}
	
	/**
	* follow project
	*/
	
	function follow_project(){
		$id = $_REQUEST['id'];
		$this->task_model->follow_project($id);
	}
	
	/**
	*  multiple task submit
	*/
	function insert_multiple_subtask(){
		$input = $this->input->post();		
		$count = count($input['m_subtask']);		
		for($i=0;$i<$count;$i++){
			if($input['m_subtask'][$i]){					
			$insert = array(
			'task_id' => $input['mtask_id'],
			'user_id' =>$this->session->userdata('user_id'),					
			'is_achieved' => 0,
			'ordering' => 0,
			'is_heading' => 0,
			'trash' => 0,
			'time_spent' => $input['hr'][$i].':'.$input['min'][$i].':'.$input['sec'][$i],
			'desc' => $input['m_subtask'][$i],
			);
			$this->task_model->save_multiple_subtask($insert);
			}
		}
		redirect('user/tasks/task_detail/'.$input['mtask_id'],'location');		
	}
	
	/*
	* @param: tasks_id
	* @return : array
	*/
	
	//adding follower to this task
	function add_follower($tasks_id){
		$input = $this->input->post();
		$this->task_model->add_follower($input['follower'],$tasks_id);
		redirect('user/tasks/task_detail/'.$tasks_id);		
	}
	
	function remove_follower(){
		$this->task_model->remove_follower($_REQUEST['id']);
	}
	
	function remove_follower_project(){
		$this->task_model->remove_follower_project($_REQUEST['id']);
	}
		
	//adding follower to project
	function add_project_follower($project_id){
		$input = $this->input->post();
		$this->task_model->add_project_follower($input['follower'],$project_id);
		redirect('user/tasks/milestones_details/'.$project_id);		
	}	
	
}
?>